home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / ppc.zip / PPC.C < prev    next >
C/C++ Source or Header  |  1988-09-17  |  16KB  |  559 lines

  1. /* PPC.C - Pretty Printer for C
  2.    Created from CB on SIMTEL20 by Richard Conn
  3.    CB was heavily modified to create PPC */
  4.  
  5. #include <stdio.h>
  6.  
  7. #define TITLE "Pretty Printer for C, Beta Test Version 1.0"
  8. #define MAXBRAC 20    /* maximum number of open braces allowed */
  9. #define IFLEVEL 20    /* maximum number of nested IFs allowed */
  10.  
  11. int slevel[IFLEVEL];
  12. int spflg[MAXBRAC][IFLEVEL];
  13. int sind [MAXBRAC][IFLEVEL];
  14. int siflev[IFLEVEL];
  15. int sifflg[IFLEVEL];
  16. int clevel = 0; /* Number of open braces */
  17. int iflev = 0; /* Number of active IFs */
  18. int ifflg = -1; /* Indicates if we have an active IF */
  19. int level = 0;
  20. int ind[IFLEVEL]; /* Indentation amount for a particular IF */
  21. int pflg[IFLEVEL]; /* Number of characters to indent for a particular IF */
  22. int eflg = 0; /* We are in an ELSE? */
  23. int paren = 0; /* Number of open parentheses */
  24. int tabcount = 4; /* Number of characters to indent */
  25. char lchar;
  26. char pchar;
  27. int aflg = 0;
  28. int stabs[MAXBRAC][IFLEVEL];
  29. int qflg = 0;
  30.  
  31. /* The following are reserved words used for special processing */
  32. char *wstr[] = {
  33.     "typedef",
  34.     "struct",
  35.     "union",
  36.     "int",
  37.     "char",
  38.     "unsigned",
  39.     "long",
  40.     "auto",
  41.     NULL
  42. };
  43. char *wrsvwds[] = {
  44.     "int",
  45.     "char",
  46.     "unsigned",
  47.     "long",
  48.     "auto",
  49.     NULL
  50. };
  51. char *wif[] = {
  52.     "if",
  53.     NULL
  54. };
  55. char *welse[] = {
  56.     "else",
  57.     NULL
  58. };
  59. char *wfor[] = {
  60.     "for",
  61.     NULL
  62. };
  63. char *wds[] = {
  64.     "case",
  65.     "default",
  66.     NULL
  67. };
  68.  
  69. /* J is the index for the next character in the STRING buffer
  70.    STRING is the buffer in which the output line is built */
  71. int j = 0;
  72. char string[400];
  73.  
  74. char cc;
  75. int sflg = 1;
  76. int bflg = 0;
  77. int peek = -1;
  78. int tabs = 0;
  79. int lastchar = ' ';
  80. int cin;
  81.  
  82. /* Input and output buffers */
  83. FILE *inpbuf;
  84. FILE *outbuf;
  85.  
  86. main(argc, argv)
  87. int argc;
  88. char *argv[];
  89. {
  90.     char bakfil[100]; /* name of current backup file */
  91.     int filno; /* number of current file */
  92.     int cont; /* local continuation flag */
  93.     int ct; /* count of local open parens */
  94.     int eflg; /* indicates we are in an ELSE */
  95.     int i;
  96.  
  97.     if (argc == 1) {
  98.         printf ("%s\n", TITLE);
  99.         printf ("  Syntax: ppc [-i#] filename.ext [filename.ext ...]\n");
  100.         printf ("  Options:\n");
  101.         printf ("     -i#  Set indentation level for files\n");
  102.         exit (0);
  103.     }
  104.  
  105.     /* Zero indentation indicator and indent character count buffers */
  106.     for (i=0; i<IFLEVEL; i++) { 
  107.         ind[i] = 0; 
  108.         pflg[i] = 0; 
  109.     }
  110.  
  111.     /* Main Loop */
  112.     for (filno=1; argv[filno]!=NULL; filno++) {
  113.  
  114.         /* Process next file (if not an option) */
  115.         if (*argv[filno] != '-') {
  116.             mkext (bakfil, argv[filno], "bak");
  117.             unlink (bakfil);
  118.             rename (argv[filno], bakfil);
  119.             if ((inpbuf = fopen (bakfil, "r")) == NULL) {
  120.                 perror (bakfil);
  121.                 exit (1);
  122.             }
  123.             if ((outbuf = fopen (argv[filno], "w")) == NULL) {
  124.                 perror (argv[filno]);
  125.                 exit (1);
  126.             }
  127.             fprintf (stderr, "  Pretty Printing %s\n", argv[filno]);
  128.             while ( (cin = getchr()) != EOF) {
  129.                 switch(cin) {
  130.                 case ' ':
  131.                 case '\t':
  132.                     if (lookup(welse) == 1) {
  133.                         gotelse ();
  134.                         if (sflg == 0 || j > 0) string[j++] =cin;
  135.                         putz ();
  136.                         sflg = 0;
  137.                         break;
  138.                     }
  139.                     if (sflg == 0 || j > 0) string[j++] =cin;
  140.                     break;
  141.                 case '\n':
  142.                     if ((eflg = lookup(welse)) == 1) gotelse();
  143.                     putz();
  144.                     fprintf (outbuf,"\n");
  145.                     sflg = 1;
  146.                     if (eflg == 1) {
  147.                         pflg[level]++;
  148.                         tabs++;
  149.                     }
  150.                     else
  151.                         if (pchar == lchar)
  152.                             aflg = 1;
  153.                     break;
  154.                 case '{':
  155.                     if (lookup(welse) == 1) gotelse();
  156.                     siflev[clevel]= iflev;
  157.                     sifflg[clevel]= ifflg;
  158.                     iflev = ifflg = 0;
  159.                     clevel++;
  160.                     if (sflg == 1 && pflg[level] != 0) {
  161.                         pflg[level]--;
  162.                         tabs--;
  163.                     }
  164.                     string[j++] =cin;
  165.                     putz ();
  166.                     getnl ();
  167.                     putz ();
  168.                     fprintf (outbuf,"\n");
  169.                     tabs++;
  170.                     sflg = 1;
  171.                     if (pflg[level] > 0) {
  172.                         ind[level] = 1;
  173.                         level++;
  174.                         slevel[level] = clevel;
  175.                     }
  176.                     break;
  177.                 case '}':
  178.                     clevel--;
  179.                     if ((iflev = siflev[clevel]-1) < 0) iflev = 0;
  180.                     ifflg = sifflg[clevel];
  181.                     putz ();
  182.                     tabs--;
  183.                     ptabs ();
  184.                     if ((peek = getchr()) == ';') {
  185.                         fprintf (outbuf, "%c;", cin);
  186.                         peek = -1;
  187.                     }
  188.                     else fprintf (outbuf,"%c", cin);
  189.                     getnl ();
  190.                     putz ();
  191.                     fprintf (outbuf,"\n");
  192.                     sflg = 1;
  193.                     if (clevel < slevel[level]) if (level > 0) level--;
  194.                     if (ind[level] != 0) {
  195.                         tabs -= pflg[level];
  196.                         pflg[level] =0;
  197.                         ind[level] = 0;
  198.                     }
  199.                     break;
  200.                 case '"':
  201.                 case '\'':
  202.                     string[j++] =cin;
  203.                     while ((cc = getc(inpbuf)) !=cin) {
  204.                         string[j++] =cc;
  205.                         if (cc == '\\') string[j++] = getc(inpbuf);
  206.                         if (cc == '\n') {
  207.                             putz();
  208.                             sflg = 1;
  209.                         }
  210.                     }
  211.                     string[j++] =cc;
  212.                     if (getnl() ==1) {
  213.                         lchar = cc;
  214.                         peek = '\n';
  215.                     }
  216.                     break;
  217.                 case ';':
  218.                     string[j++] =cin;
  219.                     putz ();
  220.                     if (pflg[level] > 0 && ind[level] == 0) {
  221.                         tabs -= pflg[level];
  222.                         pflg[level] = 0;
  223.                     }
  224.                     getnl ();
  225.                     putz ();
  226.                     fprintf (outbuf,"\n");
  227.                     sflg = 1;
  228.                     if (iflev > 0)
  229.                         if (ifflg == 1) {
  230.                             iflev--;
  231.                             ifflg = 0;
  232.                         }
  233.                         else iflev = 0;
  234.                     break;
  235.                 case '\\':
  236.                     string[j++] =cin;
  237.                     string[j++] =getchr();
  238.                     break;
  239.                 case '?':
  240.                     qflg = 1;
  241.                     string[j++] =cin;
  242.                     break;
  243.                 case ':':
  244.                     string[j++] =cin;
  245.                     if (qflg == 1) {
  246.                         qflg = 0;
  247.                         break;
  248.                     }
  249.                     if (lookup(wrsvwds) == 1) break;
  250.                     if (lookup(wds)== 0) {
  251.                         sflg = 0;
  252.                         putz ();
  253.                     }
  254.                     else {
  255.                         tabs--;
  256.                         putz ();
  257.                         tabs++;
  258.                     }
  259.                     if ((peek = getchr()) == ';') {
  260.                         fprintf (outbuf, ";");
  261.                         peek = -1;
  262.                     }
  263.